package com.wtw.test

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, SparkSession}


object Test2 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().master("local[2]").appName("sparksql").getOrCreate()
    val sc = spark.sparkContext
    sc.setLogLevel("WARN")

    import spark.implicits._

    //    实现 从 RDD 转换 得
    //    到 DataFrame， 并按“ id: 1, name: Ella, age: 36” 的 格式 打 印出 DataFrame 的 所有
    //    数据

    val file = sc.textFile("src/main/resources/employee.txt")
    val emp = file.map(x => x.split(",")).map(x => (x(0), x(1), x(2)))
    val df = emp.toDF("id", "name", "age")
    //    df.printSchema()
    //    df.show()
    df.collect().foreach(x => {
      println("id: " + x(0) + ", name: " + x(1) + ", age: " + x(2))
    })

    sc.stop()
    spark.close()
  }
}
